import pyarrow as pa
import pyarrow.parquet as pq
import json
import os
import numpy as np
from tqdm import tqdm
import pandas as pd
import numpy as np
import random

"""
处理数据，提取y用户特征
"""

'''
函数说明：
def txtToDict(filepath)->dict:  将原始训练数据读取为字典形式

def userFeatureKey(dict)->dict:  根据txtToDict返回的内容提取用户特征，格式如下：
格式：
{
    00001(uid): {
    'maxCom':maxCom, 'maxLike':maxLike, 'maxFor':maxFor, # 最大评论数、点赞数、转发数    
    'minCom':minCom, 'minLike':minLike, 'minFor':minFor, # 最小评论数、点赞数、转发数
    'aveCom':aveCom, 'aveLike':aveLike, 'aveFor':aveFor, # 平均评论数、点赞数、转发数
    'maxComLikeRate':maxComLikeRate, 'maxForLikeRate':maxForLikeRate, # 最大评论/点赞率、转发/点赞率
    'minComLikeRate':minComLikeRate, 'minForLikeRate': minForLikeRate, # 最小评论/点赞率、转发/点赞率
    'aveComLikeRate':aveComLikeRate, 'aveForLikeRate':aveForLikeRate, # 平均评论/点赞率、转发/点赞率
    'comPro':comPro, 'likePro':likePro, 'forPro':forPro # 评论数、点赞数、转发数大于平均值的概率
    }
}

def userFeatureList(dict)->dict: 根据txtToDict返回的内容提取用户特征，格式如下：
格式：
{
    00001(uid): { # 是一个list列表，内容对应上文
            [maxCom, maxLike, maxFor,
              minCom, minLike, minFor,
              aveCom, aveLike, aveFor,
              maxComLikeRate, maxForLikeRate,
              minComLikeRate, minForLikeRate,
              aveComLikeRate, aveForLikeRate,
              comPro, likePro, forPro]
    }
}

# 目前未采用的方法
def dictToJson(dict, filepath): 将字典内容保存为json文件
def jsonToDict(filepath)->dict: 将json文件中读取内容,并范围字典格式

# 目前采用的文件保存方法
def dictToNpy(dict, filepath): 将字典内容保存为npy文件
def npyToDict(filepath)-> dict: 读取npy文件，并返回字典格式
'''

# 文件头，可以只改这里
fileHead = '../Data/'

filePathTrain = fileHead + 'weibo_train_data.txt'
fileNpy = fileHead + 'train.npy'
fileUsersKeyNpy = fileHead + 'trainUsersFeatureKey.npy'
fileUsersListNpy = fileHead + 'trainUsersFeatureList.npy'

def txtToDict(filepath)->dict:
    file = open(filepath, "r", encoding='UTF-8')
    userDict = {}
    for line in file:
        a = line.strip().split()
        blog = {'time' : '', 'forward_count' : 0, 'comment_count' : 0, 'like_count' : 0, 'content' : ''}
        blog['time'] = a[2]+' '+a[3]
        blog['forward_count'] = int(a[4])
        blog['comment_count'] = int(a[5])
        blog['like_count'] = int(a[6])
        if len(a) < 8:
            # print(a)
            blog['content'] = ''
        else:
            blog['content'] = a[7]

        blogsDict = userDict.setdefault(a[0], {})
        blogsDict[a[1]] = blog

    return userDict

def dictToJson(dict, filepath):
    d = json.dumps(dict)
    with open(filepath, 'w', encoding='utf-8') as f:
        # json.dump(d, f)
        f.write(d)

def jsonToDict(filepath)->dict:
    with open(filepath, encoding='utf-8') as f:
        d = json.load(f)
    print(type(d))
    # print(d)
    return d

def dictToNpy(dict, filepath):
    np.save(filepath, dict)

def npyToDict(filepath)-> dict:
    d = np.load(filepath, allow_pickle=True).item()
    return d


def userFeatureKey(dict)->dict:

    users = dict.keys()
    d = {}
    for user in tqdm(users):
        blogs = dict[user].keys()

        comment = []
        like = []
        forward = []

        for blog in blogs:
            # print(blog['comment_count'])
            comment.append(dict[user][blog]['comment_count'])
            like.append(dict[user][blog]['like_count'])
            forward.append(dict[user][blog]['forward_count'])
        # print(111111111111111111111111)
        # sumCom, sumLike, sumFor = sum(comment), sum(like), sum(like)
        # print(sumCom, sumLike, sumFor)
        maxCom, maxLike, maxFor = max(comment), max(like), max(like)
        minCom, minLike, minFor = min(comment), min(like), min(like)
        aveCom, aveLike, aveFor = np.mean(comment), np.mean(like), np.mean(like)
        # print(aveCom, aveLike, aveFor )
        maxComLikeRate, maxForLikeRate = (0 if maxLike==0 else maxCom/maxLike), (0 if maxLike==0 else maxFor/maxLike)  # 最大评论/点赞率、转发/点赞率
        minComLikeRate, minForLikeRate = (0 if minLike==0 else minCom/minLike), (0 if minLike==0 else minFor/minLike) # 最小评论/点赞率、转发/点赞率
        aveComLikeRate, aveForLikeRate = (0 if aveLike==0 else aveCom/aveLike), (0 if aveLike==0 else aveCom/aveLike) # 平均评论/点赞率、转发/点赞率
        # print(maxComLikeRate, maxForLikeRate)
        c = sum(i > aveCom for i in comment)
        l = sum(i > aveLike for i in like)
        f = sum(i > aveFor for i in forward)
        comPro, likePro, forPro = c/len(comment), l/len(like), f/len(forward)
        # print(comPro, likePro, forPro)
        ud = {'maxCom':maxCom, 'maxLike':maxLike, 'maxFor':maxFor,
              'minCom':minCom, 'minLike':minLike, 'minFor':minFor,
              'aveCom':aveCom, 'aveLike':aveLike, 'aveFor':aveFor,
              'maxComLikeRate':maxComLikeRate, 'maxForLikeRate':maxForLikeRate,
              'minComLikeRate':minComLikeRate, 'minForLikeRate': minForLikeRate,
              'aveComLikeRate':aveComLikeRate, 'aveForLikeRate':aveForLikeRate,
              'comPro':comPro, 'likePro':likePro, 'forPro':forPro}

        d[user] = ud
    return d

def userFeatureList(dict)->dict:

    users = dict.keys()
    d = {}
    for user in tqdm(users):
        blogs = dict[user].keys()

        comment = []
        like = []
        forward = []

        for blog in blogs:
            # print(blog['comment_count'])
            comment.append(dict[user][blog]['comment_count'])
            like.append(dict[user][blog]['like_count'])
            forward.append(dict[user][blog]['forward_count'])
        # print(111111111111111111111111)
        sumCom, sumLike, sumFor = sum(comment), sum(like), sum(like)
        # print(sumCom, sumLike, sumFor)
        maxCom, maxLike, maxFor = max(comment), max(like), max(like)
        minCom, minLike, minFor = min(comment), min(like), min(like)
        aveCom, aveLike, aveFor = np.mean(comment), np.mean(like), np.mean(like)
        # print(aveCom, aveLike, aveFor )
        maxComLikeRate, maxForLikeRate = (0 if maxLike==0 else maxCom/maxLike), (0 if maxLike==0 else maxFor/maxLike)  # 最大评论/点赞率、转发/点赞率
        minComLikeRate, minForLikeRate = (0 if minLike==0 else minCom/minLike), (0 if minLike==0 else minFor/minLike) # 最小评论/点赞率、转发/点赞率
        aveComLikeRate, aveForLikeRate = (0 if aveLike==0 else aveCom/aveLike), (0 if aveLike==0 else aveCom/aveLike) # 平均评论/点赞率、转发/点赞率
        # print(maxComLikeRate, maxForLikeRate)
        c = sum(i > aveCom for i in comment)
        l = sum(i > aveLike for i in like)
        f = sum(i > aveFor for i in forward)
        comPro, likePro, forPro = c/len(comment), l/len(like), f/len(forward)
        # print(comPro, likePro, forPro)
        ud = [maxCom, maxLike, maxFor,
              minCom, minLike, minFor,
              aveCom, aveLike, aveFor,
              maxComLikeRate, maxForLikeRate,
              minComLikeRate, minForLikeRate,
              aveComLikeRate, aveForLikeRate,
              comPro, likePro, forPro]

        d[user] = ud
    return d

# filename2 = "D:\Study\Ot

if __name__ == '__main__':
    d = txtToDict("../../Data/weibo_train_data.txt")
    print(len(d.keys()))
    udkey = userFeatureKey(d)
    dictToJson(udkey, "../../Data/user_feature.json")


    # dictToNpy(d, fileNpy)
    # trainDict = npyToDict(fileNpy)
    # print(len(trainDict.keys()))

    # userFeature(trainDict)
    # udlist = userFeatureList(trainDict)
    # dictToNpy(udkey, fileUsersKeyNpy)
    # dictToNpy(udlist, fileUsersListNpy)
